﻿$(document).ready(function () {
    tinyMCE.init({
        // General options
        mode: "specific_textareas",
        editor_selector: "mceEditor",
        theme: "advanced",
        plugins: "inlinepopups,fullscreen,contextmenu,emotions,table,iespell,advlink",
        convert_urls: false,

        // Theme options
        theme_advanced_buttons1: "fullscreen,code,|,cut,copy,paste,|,undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,outdent,indent,",
        theme_advanced_buttons2: "formatselect,fontselect,fontsizeselect,|,iespell,link,unlink,sub,sup,removeformat,cleanup,charmap,emotions",
        theme_advanced_buttons3: "",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: true,
        tab_focus: ":prev,:next"
    });

    var button = $('#upload'), interval;
    new AjaxUpload(button, {
        action: '/Home/Upload',
        name: 'myfile',
        onSubmit: function (file, ext) {
            button.text('Uploading'); // change button text, when user selects file                
            this.disable();
            // Uploding -> Uploading. -> Uploading...
            interval = window.setInterval(function () {
                var text = button.text();
                if (text.length < 13) {
                    button.text(text + '.');
                } else {
                    button.text('Uploading');
                }
            }, 200);
        },
        onComplete: function (file, response) {
            button.text('Upload Image or File');

            window.clearInterval(interval);

            // enable upload button
            this.enable();
            eval("var obj = " + response);
            if (obj.Success == true) {
                var editorId = $(".mceEditor").attr("id");
                var ed = tinyMCE.get(editorId);
                ed.setContent(ed.getContent() + obj.Result);
            }
            else {
                alert(obj.Message); //display the error
            }
        }
    }); //close AjaxUpload      

    $(".extractSlug").click(function () {
        var slug = $(".slug");
        var title = $(".titleValue");
        $.post("/Admin/ExtractSlug", { title: title.val() }, function (value) {
            slug.val(value);
        });
    });
});